using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.WebControls;
using System.Windows.Media.Imaging;
using DevExpress.Data;
using DevExpress.Data.Filtering;
using DevExpress.Data.Filtering.Helpers;
using DevExpress.XtraReports.Data;
using Domain.ActiveDirectory.Accessor;
using Domain.ActiveDirectory.Model;
using Domain.Context;
using Domain.Provider;

namespace I9DocumentViewer.Controllers
{
    public class HomeController : Controller
    {
        private IDataProvider<I9Document> _i9Provider;

        public HomeController(IDataProvider<I9Document> i9Provider)
        {
            _i9Provider = i9Provider;
        }
        public ActionResult Index()
        {
            return View(_i9Provider.Entities);
        }

        public ActionResult GridViewPartialView()
        {
            return PartialView("GridViewPartialView", _i9Provider.Entities);
        }

        public ActionResult I9Document(int page, string imageName)
        {
            Stream imageStreamSource = new FileStream(System.Web.Configuration.WebConfigurationManager.AppSettings["I9Directory"] + "\\" + imageName, FileMode.Open, FileAccess.Read, FileShare.Read);
            TiffBitmapDecoder decoder = new TiffBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
            BitmapSource bitmapSource = decoder.Frames[page];
            PngBitmapEncoder encoder = new PngBitmapEncoder();

            encoder.Frames.Add(BitmapFrame.Create(bitmapSource));

            using (MemoryStream stream = new MemoryStream())
            {
                encoder.Save(stream);
                return File(stream.ToArray(), "image/png");
            }
        }
    }
}